﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Great_Eggspectations.DAL;
using Great_Eggspectations.ViewModels;
using PagedList;

namespace Great_Eggspectations.Controllers
{
    public class PartnerController : Controller
    {
        private BirdbrainContainer db = new BirdbrainContainer();
        
        // GET: /Partner/
        public ActionResult Index(string tag = "", int page = 1, int pageSize = 25)
        {
            List<Partner> partners;
            if (!string.IsNullOrEmpty(tag))
            {
                tag = tag.ToUpper();
                var partnersTemp = db.Partners.Where(p => p.CSVTags.Contains(tag)).ToList();
                partners = new List<Partner>(partnersTemp);
                foreach (var partner in partnersTemp)
                {
                    if (!partner.CSVTags.ToUpper().Split(',').Contains(tag))
                    {
                        partners.Remove(partner);
                    }
                }
            }
            else
            {
                partners = db.Partners.ToList();
            }

            var TransactionsVM = new PartnersIndexViewModel { PartnersPagedList = new PagedList<Partner>(partners, page, pageSize) };
            return View(TransactionsVM);
        }

        // GET: /Partner/Details/5
        public ActionResult Details(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Partner Partner = db.Partners.Find(id);
            if (Partner == null)
            {
                return HttpNotFound();
            }
            return View(Partner);
        }

        // GET: /Partner/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: /Partner/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "Id,Name,Address,City,State,ZIP,Phone,Email,Details,CSVTags")] Partner Partner)
        {
            if (ModelState.IsValid)
            {
                Partner.Id = Guid.NewGuid();
                Partner.Balance = 0.0m;
                db.Partners.Add(Partner);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(Partner);
        }

        // GET: /Partner/Edit/5
        public ActionResult Edit(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Partner Partner = db.Partners.Find(id);
            if (Partner == null)
            {
                return HttpNotFound();
            }
            if (db.Transactions.Any(t => t.PartnerId == id))
            {
                return RedirectToAction("FKError");
            }
            return View(Partner);
        }

        // POST: /Partner/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "Id,Name,Address,City,State,ZIP,Phone,Email,Details,CSVTags")] Partner Partner)
        {
            if (ModelState.IsValid && !db.Transactions.Any(t => t.PartnerId == Partner.Id))
            {
                db.Entry(Partner).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(Partner);
        }

        // GET: /Partner/Delete/5
        public ActionResult Delete(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Partner Partner = db.Partners.Find(id);
            if (Partner == null)
            {
                return HttpNotFound();
            }
            if (db.Transactions.Any(t => t.PartnerId == id))
            {
                return RedirectToAction("FKError");
            }
            return View(Partner);
        }

        // POST: /Partner/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(Guid id)
        {
            Partner Partner = db.Partners.Find(id);
            db.Partners.Remove(Partner);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult FKError()
        {
            return View("FKError");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
